package io.yuncheng.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.yuncheng.common.config.DictConfig;
import io.yuncheng.common.utils.PageUtils;
import io.yuncheng.common.utils.Query;
import io.yuncheng.common.utils.SysDictConstant;
import io.yuncheng.modules.sys.dao.SysOperationLogDao;
import io.yuncheng.modules.sys.entity.*;
import io.yuncheng.modules.sys.entity.*;
import io.yuncheng.modules.sys.service.SysOperationLogService;
import io.yuncheng.modules.sys.service.SysRoleService;
import io.yuncheng.modules.sys.service.SysUserRoleService;
import io.yuncheng.modules.sys.shiro.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Service("sysOperationLogService")
public class SysOperationLogServiceImpl extends ServiceImpl<SysOperationLogDao, SysOperationLogEntity> implements SysOperationLogService {
    @Resource
    private DictConfig dictConfig;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Object startDate = params.get("startDate");
        Object endDate = params.get("endDate");
        Object types = params.get("logTypes");
        String[] split = null;
        if (types != null) {
            String type = types.toString();
            split = type.split(",");
        }
        IPage<SysOperationLogEntity> page = this.page(
                new Query<SysOperationLogEntity>().getPage(params),
                new QueryWrapper<SysOperationLogEntity>().ge(startDate != null, "log_date", startDate).lt(endDate != null, "log_date", endDate).in(!ObjectUtils.isEmpty(split), "log_type", split).orderByDesc("id")
        );
        List<SysOperationLogVo> list = new ArrayList<>();
        List<SysOperationLogEntity> records = page.getRecords();
        records.forEach(a -> {
            SysOperationLogVo vo = new SysOperationLogVo();
            vo.setId(a.getId());
            vo.setLogDate(a.getLogDate());
            vo.setLogType(a.getLogType());
            vo.setLogMessage(a.getLogMessage());
            vo.setUserRule(a.getUserRule());
            vo.setUserName(a.getUserName());
            SysDictEntity sysDict = dictConfig.getSysDict(SysDictConstant.LOG_TYPE, a.getLogType() + "");
            if (sysDict != null) {
                vo.setTypeName(sysDict.getValue());
            }
            list.add(vo);

        });
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(list);
        return pageUtils;
    }

    /**
     * @param type
     * @param message
     */
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Resource
    private SysRoleService sysRoleService;
    @Override
    public void saveLog(Integer type, String message) {
        SysOperationLogEntity entity = new SysOperationLogEntity();
        entity.setLogType(type);
        entity.setLogDate(new Date());
        SysUserEntity user = ShiroUtils.getUserEntity();
        List<Long> longs = sysUserRoleService.queryRoleIdList(user.getUserId());
        if(!ObjectUtils.isEmpty(longs)){
            Collection<SysRoleEntity> sysRoleEntities = sysRoleService.listByIds(longs);
            String collect = sysRoleEntities.stream().map(SysRoleEntity::getRoleName).collect(Collectors.joining(","));
            entity.setUserRule(collect);
        }
        entity.setUserName(user.getUsername());
        entity.setLogMessage(message);
        this.save(entity);

    }

}
